#!/usr/bin/python
# coding:utf-8
from libcloud.base import singleton
from ConfigParser import ConfigParser
import os
import time
from apptrack.constants import MONGO_BACKEND, DEFAULT_BACKEND
from apptrack.utils import dynamic_import_module


class TraceDb:
    '''
    '''
    #表示该类是单例类
    #数据库引擎列表
    DRIVERS = {
       MONGO_BACKEND:"backend.mongo",
       DEFAULT_BACKEND:"backend.memory"
    }
    __metaclass__ = singleton.Singleton
    default_database_engine = None
    def __init__(self):
        self.database_engines = {}
        for name,db_engine in TraceDb.DRIVERS.items():
            database_engine_module = dynamic_import_module(db_engine)
            self.database_engines[name] = database_engine_module
  
    def set_default_backend(self,default_backend,expired_time,**kwargs):
        if not default_backend in self.database_engines:
            raise RuntimeError("error backend name %s"%default_backend)
        TraceDb.default_database_engine = default_backend
        kwargs['expired'] = expired_time
        self.database_engines[default_backend].init(**kwargs)

    def put_tag(self,span_id,key,value):
        self.database_engines[self.default_database_engine].put_tag(span_id,key,value)
            
    def put_log(self,span_id,level,msg,rings=[],**kwargs):
        self.database_engines[self.default_database_engine].put_log(span_id,level,msg,rings,**kwargs)

    def finish_span(self,span_id):
        self.database_engines[self.default_database_engine].finish_span(span_id)
            
    def start_span(self,span_id,parent_id=None,tags=[],context=None):
        self.database_engines[self.default_database_engine].start_span(span_id,parent_id,tags,context)

    def set_span_name(self,span_id,name):
        self.database_engines[self.default_database_engine].set_span_name(span_id,name)

    @classmethod
    def get_db(cls):
        return cls()
        
    def create_span_id(self):
        if not self.default_database_engine:
            raise RuntimeError("error!could not find default trace backend")
        return self.database_engines[self.default_database_engine].create_new_id()
        
    def form_span_id(self,span_id):
        return self.database_engines[self.default_database_engine].form_span_id(span_id)
        
    def get_span_info(self,span_id,append_tag=True,recursive=False):
        return self.database_engines[self.default_database_engine].get_span_info(span_id,append_tag,recursive)

    def get_span_logs(self,span_id,timestamp=None,log_ring=None,recursive=False,log_list=[]):
        return self.database_engines[self.default_database_engine].get_span_logs(span_id,timestamp,log_ring,recursive,log_list)

    def get_span_context(self,span_id):
        return self.database_engines[self.default_database_engine].get_span_context(span_id)
    
    def update_context(self,context):
        self.database_engines[self.default_database_engine].update_context(context)
        
    def get_tag(self,span_id,key,default):
        return self.database_engines[self.default_database_engine].get_tag(span_id,key,default)
        
    def create_trace(self,trace_id,service_name):
        self.database_engines[self.default_database_engine].create_trace(trace_id,service_name)
